﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Arizu.Repository;
using System.Data;
using System.Text;

namespace AiRiZu
{
    public partial class ControlPanel : System.Web.UI.Page
    {
        public string RoomListString { get; set; }
        public int RoomCount { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sql = "select * from SendRoom where Isdelete=0";
                bind(sql);
            }
            AirizuRoom airizuRoom = AirizuRoom.CreateInstance();

            DataTable dt = airizuRoom.GetRoomInfos(0);

            StringBuilder sBuilder = new StringBuilder();
            if (dt.Rows.Count > 0)
            {
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    RoomListString += dt.Rows[i]["Name"].ToString() + "<p>";
                //    RoomCount++;

                //}

                sBuilder.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"trips_list\"> <thead>");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sBuilder.Append(" <tr>");
                    sBuilder.Append("<th width=\"10\">").Append(RoomCount + 1).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append(dt.Rows[i]["Name"].ToString()).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append(dt.Rows[i]["Phone"].ToString()).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append(dt.Rows[i]["Email"].ToString()).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append(dt.Rows[i]["Province"].ToString()).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append(dt.Rows[i]["City"].ToString()).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append(dt.Rows[i]["Area"].ToString()).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append(dt.Rows[i]["Adressdesc"].ToString()).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append(dt.Rows[i]["RoomType"].ToString()).Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append("<input ID=\"" + dt.Rows[i]["SendRoomid"].ToString() + "\" type=\"button\" onclick=\"edit(this.id)\" value=\"Edit\" style=\"height: 30px;width: 68px;\"/>").Append("</th>");
                    sBuilder.Append("<th width=\"110\">").Append("<input ID=\"" + dt.Rows[i]["SendRoomid"].ToString() + "\" type=\"button\" onclick=\"release(this.id)\" value=\"Release\" style=\"height: 30px;width: 68px;\"/>").Append("</th>");
                    sBuilder.Append(" </tr>");
                    RoomCount++;
                }
                sBuilder.Append("</thead> <tbody> </tbody> </table>");

                RoomListString = sBuilder.ToString();
            }
        }

        private void bind(string sql)
        {
            DataTable dt = Main.CreateInstance().GetAllRoom(sql);
            SignDg.DataSource = dt;
            SignDg.DataBind();
        }

        /// <summary>
        /// 是否特色
        /// </summary>
        /// <param name="istese"></param>
        /// <returns></returns>
        public string Gettesestasts(string istese)
        {
            string str = "";
            if (istese.Trim() == "1")
            {
                str = "特色";
            }
            else
            {
                str = "非特色";
            }
            return str;
        }

        /// <summary>
        /// 是否特价
        /// </summary>
        /// <param name="istejia"></param>
        /// <returns></returns>
        public string Gettejiastasts(string istejia)
        {
            string str = "";
            if (istejia.Trim() == "1")
            {
                str = "特价";
            }
            else
            {
                str = "非特价";
            }
            return str;
        }

        /// <summary>
        /// 是否发布
        /// </summary>
        /// <param name="issend"></param>
        /// <returns></returns>
        public string Getsendstasts(string issend)
        {
            string str = "";
            if (issend.Trim() == "1")
            {
                str = "发布";
            }
            else
            {
                str = "未发布";
            }
            return str;
        }

        protected void SignDg_EditCommand(object source, DataGridCommandEventArgs e)
        {
            int index = int.Parse(e.Item.Cells[0].Text);
            Response.Redirect("EditRoom.aspx?roomId=" + index);
        }

        protected void SignDg_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            string id = e.Item.Cells[0].Text;
            string sqlDel = "update SendRoom set Isdelete=1 where SendRoomid=" + Convert.ToInt16(id);
            Main.CreateInstance().DelRoom(sqlDel);
            string sql = "select * from SendRoom where Isdelete=0";
            this.bind(sql);
        }

        protected void SignDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            //this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
            this.SignDg.CurrentPageIndex = e.NewPageIndex;
            string sql = "select * from SendRoom where Isdelete=0";
            bind(sql);
        }

        protected void sel_Click(object sender, EventArgs e)
        {
            string where = " and ";
            if (!string.IsNullOrEmpty(this.username.Text))
            {
                where += "Name='" + this.username.Text + "' and ";
            }
            if (!string.IsNullOrEmpty(this.useremail.Text))
            {
                where += "Email='" + this.useremail.Text + "' and ";
            }
            where += "Issend='" + this.Issend.SelectedValue + "' and IsTeJia='" + this.IsTeJia.SelectedValue + "' and IsTeSe='" + this.IsTeSe.SelectedValue + "'";
            string sql = "select * from SendRoom where Isdelete=0" + where;
            bind(sql);
        }
    }
}
